package com.jingjin.humanityservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingjin.humanityservice.service.RankingService;
import com.jingjin.humanityservice.util.ranking.cacheData.CacheDataInRedis;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: RankingServiceImpl
 * Description:
 *
 * @Author zjm
 */
@Service
@Slf4j
public class RankingServiceImpl implements RankingService {

    @Resource
    private CacheDataInRedis cacheDataInRedis;

    @Override
    public Map<String, Object> getRanking(String platform) {
        Map<String, Object> resoult;
        switch (platform) {
            case "all" :
                try {
                    // 创建一个结果映射
                    resoult = new LinkedHashMap<>();

                    // 创建一个用于存放所有平台数据的列表
                    List<Map<String, Object>> allData = new ArrayList<>();

                    // 顺序执行各个平台的数据获取任务
                    Map<String, Object> juejinData = cacheDataInRedis.getCachedData("juejin:hotSearch");
                    Map<String, Object> juejinWrappedData = new LinkedHashMap<>();
                    juejinWrappedData.put("platform", "juejin");
                    juejinWrappedData.put("data", juejinData);
                    allData.add(juejinWrappedData);

                    Map<String, Object> baiduData = cacheDataInRedis.getCachedData("baidu:hotSearch");
                    Map<String, Object> baiduWrappedData = new LinkedHashMap<>();
                    baiduWrappedData.put("platform", "baidu");
                    baiduWrappedData.put("data", baiduData);
                    allData.add(baiduWrappedData);

                    Map<String, Object> bilibiliData = cacheDataInRedis.getCachedData("bilibili:hotSearch");
                    Map<String, Object> bilibiliWrappedData = new LinkedHashMap<>();
                    bilibiliWrappedData.put("platform", "bilibili");
                    bilibiliWrappedData.put("data", bilibiliData);
                    allData.add(bilibiliWrappedData);

                    Map<String, Object> csdnData = cacheDataInRedis.getCachedData("csdn:hotSearch");
                    Map<String, Object> csdnWrappedData = new LinkedHashMap<>();
                    csdnWrappedData.put("platform", "csdn");
                    csdnWrappedData.put("data", csdnData);
                    allData.add(csdnWrappedData);

                    Map<String, Object> douyinData = cacheDataInRedis.getCachedData("douyin:hotSearch");
                    Map<String, Object> douyinWrappedData = new LinkedHashMap<>();
                    douyinWrappedData.put("platform", "douyin");
                    douyinWrappedData.put("data", douyinData);
                    allData.add(douyinWrappedData);

//                    Map<String, Object> ithomeData = cacheDataInRedis.getCachedData("ithome:hotSearch");
//                    Map<String, Object> ithomeWrappedData = new LinkedHashMap<>();
//                    ithomeWrappedData.put("platform", "ithome");
//                    ithomeWrappedData.put("data", ithomeData);
//                    allData.add(ithomeWrappedData);

                    Map<String, Object> weiboData = cacheDataInRedis.getCachedData("weibo:hotSearch");
                    Map<String, Object> weiboWrappedData = new LinkedHashMap<>();
                    weiboWrappedData.put("platform", "weibo");
                    weiboWrappedData.put("data", weiboData);
                    allData.add(weiboWrappedData);

                    Map<String, Object> krData = cacheDataInRedis.getCachedData("36kr:hotSearch");
                    Map<String, Object> krWrappedData = new LinkedHashMap<>();
                    krWrappedData.put("platform", "36kr");
                    krWrappedData.put("data", krData);
                    allData.add(krWrappedData);

                    Map<String, Object> zhihuData = cacheDataInRedis.getCachedData("zhihu:hotSearch");
                    Map<String, Object> zhihuWrappedData = new LinkedHashMap<>();
                    zhihuWrappedData.put("platform", "zhihu");
                    zhihuWrappedData.put("data", zhihuData);
                    allData.add(zhihuWrappedData);

                    // 将所有数据放入一个结果映射中
                    resoult.put("name", "all");
                    resoult.put("total", allData.size());
                    resoult.put("data", allData);

                    return resoult;

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            case "frontPage" :
                try {
                    // 创建一个结果映射
                    resoult = new LinkedHashMap<>();

                    // 创建一个用于存放所有平台数据的列表
                    List<Map<String, Object>> allData = new ArrayList<>();

                    Map<String, Object> bilibiliData = cacheDataInRedis.getCachedData("bilibili:hotSearch");
                    Map<String, Object> bilibiliWrappedData = new LinkedHashMap<>();
                    bilibiliWrappedData.put("platform", "bilibili");
                    bilibiliWrappedData.put("data", bilibiliData);
                    allData.add(bilibiliWrappedData);

                    Map<String, Object> zhihuData = cacheDataInRedis.getCachedData("zhihu:hotSearch");
                    Map<String, Object> zhihuWrappedData = new LinkedHashMap<>();
                    zhihuWrappedData.put("platform", "zhihu");
                    zhihuWrappedData.put("data", zhihuData);
                    allData.add(zhihuWrappedData);

                    Map<String, Object> krData = cacheDataInRedis.getCachedData("36kr:hotSearch");
                    Map<String, Object> krWrappedData = new LinkedHashMap<>();
                    krWrappedData.put("platform", "36kr");
                    krWrappedData.put("data", krData);
                    allData.add(krWrappedData);

                    Map<String, Object> weiboData = cacheDataInRedis.getCachedData("weibo:hotSearch");
                    Map<String, Object> weiboWrappedData = new LinkedHashMap<>();
                    weiboWrappedData.put("platform", "weibo");
                    weiboWrappedData.put("data", weiboData);
                    allData.add(weiboWrappedData);

                    Map<String, Object> douyinData = cacheDataInRedis.getCachedData("douyin:hotSearch");
                    Map<String, Object> douyinWrappedData = new LinkedHashMap<>();
                    douyinWrappedData.put("platform", "douyin");
                    douyinWrappedData.put("data", douyinData);
                    allData.add(douyinWrappedData);

                    // 将所有数据放入一个结果映射中
                    resoult.put("name", "frontPage");
                    resoult.put("total", allData.size());
                    resoult.put("data", allData);

                    return resoult;

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            case "baidu" :
                try {
                    resoult = cacheDataInRedis.getCachedData("baidu:hotSearch");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return resoult;
            case "bilibili" :
                try {
                    resoult = cacheDataInRedis.getCachedData("bilibili:hotSearch");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return resoult;
            case "csdn" :
                try {
                    resoult = cacheDataInRedis.getCachedData("csdn:hotSearch");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return resoult;
            case "douyin" :
                try {
                    resoult = cacheDataInRedis.getCachedData("douyin:hotSearch");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return resoult;
//            case "ithome" :
//                try {
//                    resoult = cacheDataInRedis.getCachedData("ithome:hotSearch");
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//                return resoult;
            case "36kr" :
                try {
                    resoult = cacheDataInRedis.getCachedData("36kr:hotSearch");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return resoult;
            case "weibo" :
                try {
                    resoult = cacheDataInRedis.getCachedData("weibo:hotSearch");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return resoult;
            case "juejin" :
                try {
                    resoult = cacheDataInRedis.getCachedData("juejin:hotSearch");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return resoult;
            case "zhihu" :
                try {
                    resoult = cacheDataInRedis.getCachedData("zhihu:hotSearch");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return resoult;
            default:
                throw new IllegalArgumentException("Unsupported platform: " + platform);
        }
    }
}
